#!/system/bin/sh

if ! command -v busybox &> /dev/null; then
  export PATH="/data/adb/magisk:/data/adb/ksu/bin:/data/adb/ap/bin:$PATH:/system/bin"
fi

# Take the current time
current_time=$(date +"%I:%M %P")

# define the settings and paths
settings="/data/adb/box/settings.ini"

# set the port numbers for tproxy and redir
tproxy_port="9898"
redir_port="9797"

# enable/disable port detection: true / false
port_detect="false"

# enable/disable IPv6: true / false
ipv6="false"

# list of available kernel binaries
bin_list=( "clash" "sing-box" "xray" "v2fly" "hysteria" )

# for run and download premium or mihomo
# put kernel premium and mihomo into /data/adb/box/bin directory
xclash_option="mihomo"
# select the client to use : clash / sing-box / xray / v2fly
bin_name="clash"

# This script is used to set the user and group for the BFM core files.
box_user_group="root:net_admin"

# redirect: tcp + udp[direct]
# tproxy: tcp + udp
# mixed: redirect[tcp] + tun[udp]
# enhance: redirect[tcp] + tproxy[udp]
# tun: tcp + udp (auto-route)
network_mode="tproxy"

# blacklist / whitelist
proxy_mode="blacklist"

# list of package names to be proxied
# android Package Name, For example: ("com.android.captiveportallogin" "com.tencent.mm")
packages_list=()

# The gid in the list will be bypassed or proxied according to the proxy_mode configuration, and the gid can be arbitrarily specified by the busybox setuidgid command
gid_list=()

# Display AP info by typing "su -c ifconfig" in terminal  "swlan+" "wlan+" "rndis+"
ap_list=( "ap+" "wlan+" "rndis+" "swlan+" )
# Ignore AP in the out list, doesn't work in fake-ip mode
ignore_out_list=()

# Set cgroup to limit memory usage
cgroup_memcg="false"
memcg_limit="25M"

# Set cgroup to cpuset usage
cgroup_cpuset="false"

# Set cgroup to blkio usage
cgroup_blkio="false"

# Set box directory variables
box_dir="/data/adb/box"
box_run="${box_dir}/run"
box_log="${box_run}/runs.log"
box_pid="${box_run}/box.pid"
bin_dir="${box_dir}/bin"
bin_path="${bin_dir}/${bin_name}"

# if you don't want to generate the /run/${bin_name}.log file (because it will grow in size over time), replace "${box_run}/${bin_name}.log" to "/dev/null"
bin_log="${box_run}/${bin_name}.log"
# bin_log="/dev/null"

system_packages_file="/data/system/packages.list"
uid_list=("/data/adb/box/run/appuid.list")

# Update subscription & geox
# Set update interval using cron, for more information: https://crontab.guru/
# set 'true' to enable Cron job
interva_update="0 0,6,12,18 * * *" # will update every hour 00,06,12,18
run_crontab="false"

# type "su -c /data/adb/box/scripts/box.tool geox" to update geox
update_geo="true"

# if "renew=true" it will update config.yaml not subscription configuration
# type "su -c /data/adb/box/scripts/box.tool subs" to update subscription
renew=false
update_subscription="false"
# ex: clash subscription url
subscription_url_clash="https://nodefree.org/dy/$(date +%Y)/$(date +%m)/$(date +%Y%m%d).yaml" # this is just an example.

# clash configuration
name_clash_config="config.yaml"
clash_config="${box_dir}/clash/${name_clash_config}"
# clash subscription 
name_provide_clash_config="subscription.yaml"
clash_provide_config="${box_dir}/clash/provide/${name_provide_clash_config}"
# support rules specified in subscriptions, This is useful when the subscription contains customized rules as well.
custom_rules_subs="false"
name_provide_clash_rules="rules.yaml"
clash_provide_rules="${box_dir}/clash/provide/${name_provide_clash_rules}"

# sing configuration
name_sing_config="config.json"
sing_config="${box_dir}/sing-box/${name_sing_config}"

normal="\033[0m"
orange="\033[1;38;5;208m"
red="\033[1;31m"
green="\033[1;32m"
yellow="\033[1;33m"
blue="\033[1;34m"

# eg: notify $title $content
notify() {
  # using Xposed edge app toast
  am start -a android.intent.action.VIEW -n com.jozein.xedgepro/.ui.ActivityPerformAction --ei __0 111 --es __1 "$2" >/dev/null 2>&1

  # using system notification
  # su -lp "2000" -c "cmd notification post -S messaging --conversation '$1' --message '$1':'$2' 'Tag' '$(echo $RANDOM)' " >/dev/null 2>&1
}

log() {
  # Selects the text color according to the parameters
  case $1 in
    Info) color="${blue}" ;;
    Error) color="${red}" ;;
    Warning) color="${yellow}" ;;
    *) color="${green}" ;;
  esac
  # Add messages to time and parameters
  message="${current_time} [$1]: $2"
  if [ -t 1 ]; then
    # Prints messages to the console
    echo -e "${color}${message}${normal}"
  else
    # Print messages to a log file
    echo "${message}" >> ${box_log} 2>&1
  fi
  
  if [[ $TOAST ]]; then
      notify "box_for_root" "${message}"
  fi

}